#include "CannySegmentation.h"
#include <FL/fl_ask.H>

CannySegmentation::CannySegmentation(void)
{



    m_CannyFilter = CannyFilterType::New();
  m_CannyFilter->SetInput(  m_FastMarchingFilter->GetOutput() );
  m_CannyFilter->SetFeatureImage(   m_CastImageFilter2DsTo2Df->GetOutput() );

  m_CannyFilter->SetMaximumRMSError( 0.02 );
  m_CannyFilter->SetNumberOfIterations( 20 );
  m_CannyFilter->UseImageSpacingOn();
  m_CannyFilter->SetVariance( 1.0 );

  	  m_CastImageFilter2DfTo3Df1->SetInput( m_CannyFilter->GetOutput() );
	    m_CannyEdgesThresholdFilter = ThresholdFilterType::New();
  m_CannyEdgesThresholdFilter->SetInput( m_CannyFilter->GetCannyImage() );
  m_CannyEdgesThresholdFilter->SetUpperThreshold( itk::NumericTraits<InternalPixelType>::Zero ); 
  m_CannyEdgesThresholdFilter->SetLowerThreshold( itk::NumericTraits<InternalPixelType>::NonpositiveMin() ); 
  m_CannyEdgesThresholdFilter->SetInsideValue( 0 );
  m_CannyEdgesThresholdFilter->SetOutsideValue( 1 );

  
  m_ThresholdFilter->SetInput( m_CannyFilter->GetOutput() );
  m_ThresholdFilter->SetUpperThreshold( itk::NumericTraits<InternalPixelType>::Zero ); 
  m_ThresholdFilter->SetLowerThreshold( itk::NumericTraits<InternalPixelType>::NonpositiveMin() ); 
  m_ThresholdFilter->SetInsideValue(   1 );
  m_ThresholdFilter->SetOutsideValue(  0 );
   m_VtkImporter1 = vtkImageImport::New();
  m_VtkImporter2 = vtkImageImport::New();

  m_ContourFilter = vtkContourFilter::New();

  m_PolyDataWriter = vtkPolyDataWriter::New();

  m_ItkExporter1 = ExportFilterType::New();
  m_ItkExporter2 = ExportFilterType::New();

  m_ProbeFilter = vtkProbeFilter::New();

  this->ConnectPipelines( m_VtkImporter1, m_ItkExporter1 );
  this->ConnectPipelines( m_VtkImporter2, m_ItkExporter2 );
 m_ItkExporter2->SetInput( m_CastImageFilter2DfTo3Df2->GetOutput() );
   m_ItkExporter1->SetInput( m_CastImageFilter2DfTo3Df1->GetOutput() );

  m_ContourFilter->SetInput( m_VtkImporter1->GetOutput() );  

  m_ContourFilter->SetValue( 0, 0.0 ); // Value of the ZeroSet.

  m_ProbeFilter->SetInput( m_ContourFilter->GetOutput() );
  m_ProbeFilter->SetSource( m_VtkImporter2->GetOutput() );
  
  m_PolyDataWriter->SetInput( m_ProbeFilter->GetOutput() );


}

CannySegmentation::~CannySegmentation(void)
{
	  m_PolyDataWriter->Delete();

  m_ContourFilter->Delete();

  m_VtkImporter1->Delete();
  m_VtkImporter2->Delete();
}

void
CannySegmentation
::RunCanny( void )
{

  // update the marching filter
  try
    {
    this->ComputeEdgePotential();
    this->ComputeFastMarching();
    this->ShowStatus("Computing USSeg Filter");
    m_CannyFilter->UpdateLargestPossibleRegion();
    m_CannyEdgesThresholdFilter->UpdateLargestPossibleRegion();
    }
  catch( itk::ExceptionObject & exp )
    {
    fl_alert( exp.GetDescription() );
    }

}
void
CannySegmentation 
::SetThreshold( double value )
{
  m_CannyFilter->SetThreshold( value );
}

void CannySegmentation
::ConnectPipelines( vtkImageImport * importer, ExportFilterType * exporter )
{
  importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
  importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
  importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
  importer->SetSpacingCallback(exporter->GetSpacingCallback());
  importer->SetOriginCallback(exporter->GetOriginCallback());
  importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
  importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
  importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
  importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
  importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
  importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
  importer->SetCallbackUserData(exporter->GetCallbackUserData());
}

